package com.mega.fe.common;

import com.mega.fe.Unravel;
import com.mega.fe.mixins.EntityTickListAccessor;
import com.mega.fe.util.BaseInit;
import com.mega.fe.util.MEGA;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.entity.EntityTickList;

import java.util.function.Consumer;

public class FuckEntityList extends EntityTickList {
    public static int klass_ptr;

    static {
        try {
            klass_ptr = MEGA.KLASS_PTR(FuckEntityList.class);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void forEach(Consumer<Entity> p_156911_) {
        if (p_156911_ == null)
            return;
        Int2ObjectMap<Entity> iterated = ((EntityTickListAccessor)this).iterated();
        Int2ObjectMap<Entity> active = ((EntityTickListAccessor)this).active();
        if (iterated != null) {
            throw new UnsupportedOperationException("Only one concurrent iteration supported");
        } else {
            iterated = active;

            try {

                for (Entity $$1 : active.values()) {
                    if ($$1 == null) {
                        break;
                    }
                    p_156911_.accept($$1);
                }
            } finally {
                iterated = null;
            }

        }
    }

    @Override
    public void add(Entity p_156909_) {
        if (BaseInit.isDeath(p_156909_))
            return;
        super.add(p_156909_);
    }

    @Override
    public void remove(Entity p_156913_) {
        if (p_156913_ == null) Unravel.out("AttackUtil:target is null");
        if (p_156913_.getId() < 0) Unravel.out("Entity:%s has been removed by world", p_156913_.getCustomName());

        super.remove(p_156913_);
    }

    @Override
    public boolean contains(Entity p_156915_) {
        if (BaseInit.isDeath(p_156915_))
            return false;
        return super.contains(p_156915_);
    }
}
